import services from '@/services/demo';
import { Button, Divider, Drawer, message,Switch } from 'antd';
import React, { useRef, useState,useEffect } from 'react';

const { switchList,updateSwitch } =
  services.UserController;

const TableList: React.FC<unknown> = () => {
  const [check, setCheck] = useState<boolean>(true);// 列表数据
  const [checkId, setCheckId] = useState<string>('');// 
  useEffect(()=>{
    switchList().then(res=>{
      if(res.code=='200'){
        if(res.data[0]){
          setCheck(res.data[0].isOpen==1)
          setCheckId(res.data[0].id)
          // message.success('设置成功')
        }
      }
    })
  },[])
  const onChange = (checked: boolean)=> {
    console.log(checked);
    setCheck(checked)
    updateSwitch({id:checkId,isOpen:checked?1:2}).then(res=>{
      if(res.code=='200'){
      }
    })
  }
  return (
    <div style={{height:'90vh',backgroundColor:'#fff',padding:'20px'}}>
      <h1>开关控制</h1>
      <div style={{display:'flex',padding:'20px',alignItems:'center'}}>
        <div style={{marginRight:'10px',fontSize:'16px'}}>用户是否可评论</div>
        <div>
          <Switch checked={check} onChange={onChange} />
        </div>
      </div>
    </div>
  );
};

export default TableList;
